import { dictionaryGetTypeList, dictionaryGetCatalogByType, dictionaryGetByNames } from "@/api/dataDict";
export default {
  data() {
    return {
      treeData: [],
      selectTree: {
        label: "",
        id: "",
        names: "",
        status: ""
      },
      props: {
        label: "label",
        children: "children",
        isLeaf: "leaf"
      }
    };
  },
  methods: {
    async loadNode(node, resolve) {
      const { data, level } = node;
      let treeData = [];
      if (level === 0) {
        const data = await dictionaryGetTypeList() || [];
        treeData = data.map(item => {
          return {
            label: item
          };
        });
        return resolve(treeData);
      }
      if (level === 1) {
        let data2 = await dictionaryGetCatalogByType({ type: data.label }) || [];
        data2 = data2.map(item => {
          const { id, remark, name } = item;
          return {
            id, name,
            label: remark,
            leaf: true,
            level
          };
        });
        resolve(data2 || []);
      }
    },
    async nodeClick(data) {
      const { level, name, label, id } = data;
      if (level === 1) {
        this.selectTree = {
          label, names: name,
          id, status: ""
        };
        this.search({
          names: name,
          status: ""
        });
      }
    },
    async search(data = this.selectTree) {
      this.dataGrid.loading = true;
      try {
        const _data = await dictionaryGetByNames(data) || [];
        this.dataGrid.loading = false;
        if (_data[data.names] && _data[data.names].length) {
          this.dataGrid.data = _data[data.names].map(item => {
            const { status } = item;
            item.statusName = status === 0 ? "启用" : "禁用";
            return item;
          });
        } else {
          this.dataGrid.data = [];
        }
      } catch (error) {
        this.dataGrid.loading = false;
      }
    },
    handleNodeClick(data) {
      console.log(data);
    }
  }
};
